package com.ruoyi.airsystem.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.airsystem.domain.AirFamilies;
import com.ruoyi.airsystem.service.IAirFamiliesService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 家庭管理Controller
 * @date 2025-04-18
 */
@RestController
@RequestMapping("/airsystem/families")
public class AirFamiliesController extends BaseController
{
    @Autowired
    private IAirFamiliesService airFamiliesService;

    /**
     * 查询家庭管理列表
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:list')")
    @GetMapping("/list")
    public TableDataInfo list(AirFamilies airFamilies)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员(ID=1)，则设置查询条件中的userId为当前用户ID
        if (userId != 1L) {
            airFamilies.setUserId(userId);
        }

        startPage();
        List<AirFamilies> list = airFamiliesService.selectAirFamiliesList(airFamilies);
        return getDataTable(list);
    }

    /**
     * 导出家庭管理列表
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:export')")
    @Log(title = "家庭管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AirFamilies airFamilies)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员(ID=1)，则设置查询条件中的userId为当前用户ID
        if (userId != 1L) {
            airFamilies.setUserId(userId);
        }

        List<AirFamilies> list = airFamiliesService.selectAirFamiliesList(airFamilies);
        ExcelUtil<AirFamilies> util = new ExcelUtil<AirFamilies>(AirFamilies.class);
        util.exportExcel(response, list, "家庭管理数据");
    }

    /**
     * 获取家庭管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:query')")
    @GetMapping(value = "/{familyId}")
    public AjaxResult getInfo(@PathVariable("familyId") Long familyId)
    {
        AirFamilies airFamily = airFamiliesService.selectAirFamiliesByFamilyId(familyId);
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        // 如果不是管理员(ID=1)，且家庭记录不属于当前用户，则返回无权限
        if (userId != 1L && (airFamily == null || !userId.equals(airFamily.getUserId()))) {
            return AjaxResult.error("没有权限查看此家庭信息");
        }
        return success(airFamily);
    }

    /**
     * 新增家庭管理
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:add')")
    @Log(title = "家庭管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AirFamilies airFamilies)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员(ID=1)，则强制设置用户ID为当前用户
        if (userId != 1L) {
            airFamilies.setUserId(userId);
        }

        return toAjax(airFamiliesService.insertAirFamilies(airFamilies));
    }

    /**
     * 修改家庭管理
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:edit')")
    @Log(title = "家庭管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AirFamilies airFamilies)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员(ID=1)，需要验证要修改的家庭是否属于当前用户
        if (userId != 1L) {
            AirFamilies existing = airFamiliesService.selectAirFamiliesByFamilyId(airFamilies.getFamilyId());
            if (existing == null || !userId.equals(existing.getUserId())) {
                return AjaxResult.error("没有权限修改此家庭信息");
            }
            // 强制设置用户ID为当前用户，防止被修改
            airFamilies.setUserId(userId);
        }

        return toAjax(airFamiliesService.updateAirFamilies(airFamilies));
    }

    /**
     * 删除家庭管理
     */
    @PreAuthorize("@ss.hasPermi('airsystem:families:remove')")
    @Log(title = "家庭管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public AjaxResult remove(@PathVariable Long[] familyIds)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员(ID=1)，需要验证要删除的所有家庭是否属于当前用户
        if (userId != 1L) {
            for (Long familyId : familyIds) {
                AirFamilies existing = airFamiliesService.selectAirFamiliesByFamilyId(familyId);
                if (existing == null || !userId.equals(existing.getUserId())) {
                    return AjaxResult.error("没有权限删除家庭ID为 " + familyId + " 的记录");
                }
            }
        }

        return toAjax(airFamiliesService.deleteAirFamiliesByFamilyIds(familyIds));
    }
}
